begintownscript;

 begintownscript;	

 variables;
int i,j,k,r1,choice;
short charwho;
string char1;

body;
beginstate INIT_STATE;
	set_name(20,"Graybeard");

	add_char_to_group(22,1);
	add_char_to_group(23,1);
	add_char_to_group(24,1);
	 break;	

beginstate START_STATE;


	if (get_ran(1,0,100) < 8)
		text_bubble_on_char(20,"I do not want to fight.");
	if (get_ran(1,0,100) < 8)
		text_bubble_on_char(20,"Human! We talk.");

break;	

beginstate EXIT_STATE;
break;

beginstate 10;
	reset_dialog();
	add_dialog_str(0,"At the end of this long tunnel, natural light winks at you. Do you want to go back to the ogre village?",0);
	add_dialog_choice(0,"Not yet.");
	add_dialog_choice(1,"Yes.");
	choice = run_dialog(1);

	if (choice == 1)
		block_entry(1);

	if (choice == 2)
		move_to_new_town(4,9,39);
 break;

beginstate 11;
	reset_dialog();
	add_dialog_str(0,"The surface of the portal vibrates erratically. The picture to the other side remains stable for about a second and then it is lost in incomprehensible waves for several seconds. The vibrations are not that much of a bother.",0);
	add_dialog_str(1,"Even when the image is clear you cannot see what is on the other side. All you see is a dark empty space and a stone wall. No matter what lies beyond this portal, stepping through it will be an unpleasant enterprise.",0);
	add_dialog_choice(0,"Remain in the caves.");
	add_dialog_choice(1,"Step through the portal.");
	choice = run_dialog(1);

	if (choice == 1)
		block_entry(1);

	if (choice == 2) {
		play_sound(10);
		message_dialog("You step into the portal. Its magic tears your body apart and transports you into a sickening vertigo towards your destination on the other side.","");
		move_to_new_town(6,16,28);
}

 break;

beginstate 12;
	if (get_flag(5,7) == 0) {
		message_dialog("You see a big bonfire. A large stooped over figure stands in front of it. Its bushy white beard reaches down to its navel. Its eyes meet yours and it does not shy away from your gaze. This ogre is self-confident and fearless.","It stands still and waits for you, but in no way does it act aggressively towards you.");
		set_flag(5,7,1);
}
 break;

beginstate 13;
	if (char_ok(20) == TRUE) {
		block_entry(1);
	
		if (char_on_loc(20,8) == 20)
			message_dialog("Graybeard blocks your passage. _I ask you not to enter these doors. The ogres hiding there are afraid of you. I do not want to frighten them anymore._","");

		if (get_flag(5,14) == 1)
			message_dialog("The ogres loyal to Graybeard hide behind these doors. It would be best for you not to spook them with your presence. You back away from the door.","");
}
 break;

beginstate 14;
	if (get_flag(5,8) == 0) {
		block_entry(1);
		message_dialog("A large shape moves in the entrance to the cave ahead. It is too far away for you to make it out clearly. It begins to chant a spell you do not recognize. However, the effect of the spell is immediate.","You can feel the magic crackling all around you. Huge swarms of flies emerge from the darkness in all directions. The buzzing is incredible. You count at least half a dozen separate swarms. The shape ahead has disappeared.");
		set_flag(5,8,1);
		activate_hidden_group(1);
		add_char_to_group(27,3);
		add_char_to_group(28,3);
		add_char_to_group(29,3);
		add_char_to_group(30,3);
		add_char_to_group(31,3);
		add_char_to_group(32,3);
		add_char_to_group(33,3);
		add_char_to_group(34,3);
		alert_char(1003);
}
 break;

beginstate 15;
	if (get_flag(5,9) == 0) {
		reset_dialog();
		add_dialog_str(0,"You enter a high chamber. It does not look artificially created which makes it even more impressive. Two brightly burning braziers illuminate the chamber. The light dances on the reflective minerals on the walls.",0);
		add_dialog_str(1,"The ogres have decorated the chamber with their customary savage ways. Animal heads have been placed on spikes. Little mountains have been created out of humanoid skulls. Some kind of incense hangs in the air.",0);
		add_dialog_str(2,"In the center of the chamber the ground forms into a small level natural platform. On its center there is a rug. On the rug there sits an ogre. It is Angerfist. His head is bowed as if in meditation, but he soon raises it.",0);
		add_dialog_str(3,"True to his name, there is anger in his eyes. _No more talk!_ He bellows as he gets to his feet. _It is Angerfist or puny humans who die now!_ The time for diplomacy has passed.",0);
		run_dialog(1);
		set_flag(5,9,1);
}
	if ((get_flag(6,5) == 1) && (get_flag(5,10) == 0)) {
		if (get_flag(152,7) != 1)
			activate_hidden_group(3);
		if (get_flag(2,8) == 1) {
			message_dialog("An ogre jumps out of the shadows. You reach for your weapons, but stop before long. You recognize this ogre. It is Chainhound, or maybe Ragehound, the ogre you freed from the ogre fort.","He does not look particularly raging. In fact, he is happy.");
			activate_hidden_group(2);
			set_name(35,"Joyhound");
			force_instant_terrain_redraw();
			pause(3);
			relocate_character(35,6,38);
			play_sound(49);
			force_instant_terrain_redraw();
			pause(3);
			relocate_character(35,6,37);
			play_sound(49);
			force_instant_terrain_redraw();
			pause(3);
			relocate_character(35,6,36);
			play_sound(49);
			force_instant_terrain_redraw();
			pause(3);
			relocate_character(35,6,35);
			play_sound(49);
			force_instant_terrain_redraw();
			pause(3);
			text_bubble_on_char(35,"You kill Angerfist!");
			force_instant_terrain_redraw();
			pause(18);
			text_bubble_on_char(35,"");
			force_instant_terrain_redraw();
			text_bubble_on_char(35,"Me happy!");
			force_instant_terrain_redraw();
			pause(18);
			set_flag(5,10,1);
			begin_talk_mode(16);
}
}
	if ((get_flag(6,5) == 5) && (get_flag(5,10) == 0)) {
		if (get_flag(152,7) != 1)
			activate_hidden_group(3);
			set_flag(5,10,1);
}
	
 break;

beginstate 16;
	if (get_flag(5,10) == 0) {
		message_dialog("You inspect the panicking group of ogres in this cave. They are all running around the cave, trying to get away from you. But there is only one exit and you are blocking it. None of the ogres look like warriors.","In fact, most of them are clearly just children. You notice that one of the ogres, who is clearly a wizard, is not as panicky as the rest. He tries to organize the ogres: _Kill the humans! Before they kill us!_");
		set_flag(5,10,1);
}
 break;

beginstate 17;
	if (get_flag(5,13) == 1)
		end();

	reset_dialog();
	add_dialog_str(0,"There is a book on the pedestal; a spell tome to be precise. You can sense the magic coming from it. You anxiously open the tome to study it. Fortunately for you, you realized what you were looking at before plunging head first into it.",0);
	add_dialog_str(1,"The tome is written in the large crude handwriting of the ogres and the very first thing on it is a curse that would have sapped your magical energies had you been foolish enough to conjure it. The second conjuration in it would have burned you.",0);
	add_dialog_str(2,"The third one you do not recognize offhand. There are some fifty pages worth of conjurations in the tome. Surely they cannot all be traps to harm the reader.",0);
	add_dialog_choice(0,"(Read the tome.)");
	add_dialog_choice(1,"My rune reading is not that good. (Leave)");
	choice = run_dialog(1);

	if ((choice == 1) && (get_skill_total(42) >= 13)) {

		message_dialog("It certainly can be nothing but traps for the reader; and it almost is. Amidst all the deadly spells and damaging magical effects you find but one spell that suits your needs: Ice Lances. The tome allows a mage to learn the spell.","But even if the mage already knows it they might improve their knowledge of the spell.");
		charwho = 0;
		reset_dialog();
		while (charwho < party_size()) {

		clear_buffer();
		append_char_name(charwho);
		if ((get_stat(charwho,11) >= 5) && (get_spell_level(charwho,0,6) > 0) && (get_spell_level(charwho,0,6) < 4)) {
			append_string(" knows the spell Ice Lances better now.");
			change_spell_level(charwho,0,6,1);
				}
		if ((get_stat(charwho,11) >= 5) && (get_spell_level(charwho,0,6) == 0)) {
			append_string(" learns a new spell - Ice Lances.");
			change_spell_level(charwho,0,6,1);
											}
		if ((get_stat(charwho,11) >= 5) && (get_spell_level(charwho,0,6) > 4))
			append_string(" learns nothing new.");
		if (get_stat(charwho,11) < 5)
			append_string(" is not skilled enough in magic.");
		get_buffer_text(char1);
		add_dialog_str(charwho,char1,0);
		

		charwho = charwho + 1;

		
}
		if (charwho == party_size()) {
			run_dialog(1);
			set_flag(5,13,1);
			award_party_xp(25,13);
			}
}
	if ((choice == 1) && (get_skill_total(42) < 13)) {
		message_dialog("You thought you found a spell hidden among all the traps, but it turned out you were wrong. You were simply not skilled enough in rune reading to avoid setting off one of the traps.","You are no longer alone in the cave!");
		activate_hidden_group(4);

}

 break;
